#include "stdafx.h"
#include "oil_model_editor.h"

using namespace std;
static const QColor indexUsedColor = QColor(167, 211, 92);

OilModelEitor::OilModelEitor(sim *_main) : main(_main)
	//: QDialog(pwgt, Qt::WindowTitleHint | Qt::WindowSystemMenuHint)
{
	ui.setupUi(this);
	connect(ui.bAdd, SIGNAL(clicked), this, SLOT(on_bAdd_clicked));
	connect(ui.bRemove, SIGNAL(clicked), this, SLOT(on_bRemove_clicked));
	connect(ui.lstModel, SIGNAL(currentRowChanged(int)), this, SLOT(displayOilModel(int)));
	connect(ui.bUse, SIGNAL(clicked), this, SLOT(on_bUse_clicked));
}

OilModelEitor::~OilModelEitor(){
}

void OilModelEitor::on_bAdd_clicked(){
	QString name = QInputDialog::getText(this,"Creating oil model...", "Enter a model name");
	if (name == ""){
		QMessageBox::critical(0, "Error", "Empty name is not valid!", QMessageBox::Ok);	
		return;
	}
	for (int i = 0; i < ui.lstModel->count(); i++)
		if(ui.lstModel->item(i)->text() == name){
			QMessageBox::critical(0, "Error", "That name already exists!", QMessageBox::Ok);	
			return;
		}
		oilModelList.push_back(new OilModel(main, name));
		ui.lstModel->addItem(name);
		ui.lstModel->setCurrentRow(ui.lstModel->count() - 1);
		displayOilModel(ui.lstModel->currentRow());	
		main->log("Oil model: '" + name + "' has been added");
}


void OilModelEitor::on_bRemove_clicked(){
	if(QMessageBox::question(0, "Removing oil model...", "Remove the oil model: '" + ui.lstModel->currentItem()->text() + "'?",
		QMessageBox::Yes |QMessageBox::No) == QMessageBox::Yes){
			oilModelList.erase(oilModelList.begin() + ui.lstModel->currentRow());
			QString name = ui.lstModel->currentItem()->text();
			int iii = ui.lstModel->currentRow();
			//QListWidgetItem *ii = ui.lstModel->takeItem(ui.lstModel->currentItem());
			//ui.lstModel->removeItemWidget(ui.lstModel->currentItem());
			//ui.lstModel->update();
			
			delete ui.lstModel->takeItem(ui.lstModel->currentRow());
			displayOilModel(ui.lstModel->currentRow());
			main->log("Oil model: '" + name + "' has been removed");
	}
}

void OilModelEitor::on_bUse_clicked(){
	for(int i = 0; i < ui.lstModel->count(); i++){
		if (i == ui.lstModel->currentRow()){
			ui.lstModel->item(i)->setBackgroundColor(indexUsedColor);
		}else{
			ui.lstModel->item(i)->setBackgroundColor(Qt::white);
		}
	}
}

void OilModelEitor::displayOilModel(int index){
	setModel(oilModelList[index]->model);
}

void OilModelEitor::setModel(PropertiesModel *model){
	ui.trParameters->setModel(model);
	ui.trParameters->setItemDelegate(new Delegate(main));
	ui.trParameters->setStyleSheet("alternate-background-color: rgb(190, 234, 235)");
	for(int i = 3; i <= 6; i++)
		ui.trParameters->hideColumn(i);
	ui.trParameters->header()->setStretchLastSection(false);
	ui.trParameters->header()->setResizeMode(1, QHeaderView::Stretch);   
	ui.trParameters->setColumnWidth(0, 120);
	ui.trParameters->setColumnWidth(2, 40);
}


void OilModelEitor::setData(vector<OilModel*> _oilModelList, int _index){
	oilModelList = _oilModelList;
	for (int i = 0; i < oilModelList.size(); i++){
		ui.lstModel->addItem(oilModelList[i]->getName());
	}
	if (_index >= 0 && _index < ui.lstModel->count())
		ui.lstModel->item(_index)->setBackgroundColor(QColor(167, 211, 92));
}

vector<OilModel*> OilModelEitor::getData(){
	return oilModelList;
}

int OilModelEitor::getIndex(){
	int index_of_model_used;
	for(index_of_model_used = 0; index_of_model_used < ui.lstModel->count(); index_of_model_used++)
		if (ui.lstModel->item(index_of_model_used)->backgroundColor() == indexUsedColor)
			break;
	if (index_of_model_used == ui.lstModel->count()) index_of_model_used = -1;
	return index_of_model_used;
}